<?PHP
/*
  Plugin for download from tu.tv (http://tu.tv/)
  Developed by seth <xd dot seth at gmail dot com - http://elrincondeseth.wordpress.com> based on OzX's code <http://foro.undersecurity.net>
*/

$plugins[] = array('tutv','@(http://|http://www.)tu.tv/videos/.+@');

function tutv_download($url, $extra)
{
    $html = downloadUrl($url);
    if(($html['code']<200) or ($html['code']>299)) {
        return array(
                        'status'       => STATUS_ERROR,
                        'status_error' => ($html['code'] == 302) ? 'File not found' : 'Download error. Response code #'.$html['code'],
                        'content'      => ''
        );
    }
    
    preg_match_all("#RunPlayer.*'.*','(.*?)','.*#", $html['content'], $match, PREG_SET_ORDER);
    $fileUrl = base64_decode($match[0][1]);
    $file = downloadUrlRecursive($fileUrl,'GET','','','','');

    if(($file['code']<200) or ($file['code']>299)) {        
        return array(
                        'status'       => STATUS_ERROR,
                        'status_error' => 'Download error. Response code #'.$file['code'],
                        'content'      => ''
        );
        
    }else{
        return array(
                        'status'  => STATUS_DOWNLOADED,
                        'content' => $file['content']
        );
    }
}

function tutv_getName($url)
{
    return strpos($url,'?') ? strstr(basename($url),'?',true).'.flv' : basename($url).'.flv';
}

?>

